package com.pds.web.spotCheck.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pds.common.utils.DateUtils;
import com.pds.common.utils.SecurityUtils;
import com.pds.web.spotCheck.entity.PdsEnvironmentExamine;
import com.pds.web.spotCheck.mapper.PdsEnvironmentExamineMapper;
import com.pds.web.spotCheck.service.PdsEnvironmentExamineService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 监管部门考核机构表
 *
 * @author srf sunlightcs@gmail.com
 * @since 1.0.0 2024-02-02
 */
@Service("pdsEnvironmentExamineService")
public class PdsEnvironmentExamineServiceImpl extends ServiceImpl<PdsEnvironmentExamineMapper, PdsEnvironmentExamine> implements PdsEnvironmentExamineService {
    @Resource
    private PdsEnvironmentExamineMapper pdsEnvironmentExamineMapper;

    @Override
    public List<PdsEnvironmentExamine> selectList(PdsEnvironmentExamine pdsEnvironmentExamine) {
        QueryWrapper<PdsEnvironmentExamine> wrapper = new QueryWrapper<>();
        Long organizationId = pdsEnvironmentExamine.getOrganizationId();
        String examineContent = pdsEnvironmentExamine.getExamineContent();
        String examineResult = pdsEnvironmentExamine.getExamineResult();
        Date examineDate = pdsEnvironmentExamine.getExamineDate();
        if(!Objects.isNull(organizationId)){
            wrapper.eq("organization_id",organizationId);
        }
        if(!Objects.isNull(examineContent) && !examineContent.isEmpty()){
            wrapper.like("examine_content",examineContent);
        }
        if(!Objects.isNull(examineResult) && !examineResult.isEmpty()){
            wrapper.eq("examine_result",examineResult);
        }
        if(!Objects.isNull(examineDate)){
            wrapper.eq("examine_date",examineDate);
        }
        return pdsEnvironmentExamineMapper.selectList(wrapper);
    }

    @Override
    public PdsEnvironmentExamine queryById(Long id) {
        return pdsEnvironmentExamineMapper.selectById(id);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public PdsEnvironmentExamine insert(PdsEnvironmentExamine pdsEnvironmentExamine) {
        Date date = DateUtils.parseDate(DateUtils.getTime());
        /*Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
        String nickName = SecurityUtils.getLoginUser().getUser().getNickName();
        pdsEnvironmentExamine.setUserId(userId);
        pdsEnvironmentExamine.setCreateBy(nickName);*/
        pdsEnvironmentExamine.setStatus("1");
        pdsEnvironmentExamine.setCreateTime(date);
        pdsEnvironmentExamineMapper.insert(pdsEnvironmentExamine);
        return pdsEnvironmentExamine;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public Boolean update(PdsEnvironmentExamine pdsEnvironmentExamine) {
        Date date = DateUtils.parseDate(DateUtils.getTime());
        /*String nickName = SecurityUtils.getLoginUser().getUser().getNickName();
        pdsEnvironmentExamine.setUpdateBy(nickName);*/
        pdsEnvironmentExamine.setUpdateTime(date);
        return pdsEnvironmentExamineMapper.updateById(pdsEnvironmentExamine) > 0;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public Boolean deleteById(Long id) {
        return pdsEnvironmentExamineMapper.deleteById(id) > 0;
    }
}